package com.generator.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generator.shop.entity.Shop;
import com.generator.shop.dao.ShopDao;
import com.generator.shop.service.ShopService;
import com.generator.utils.PageData;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商店信息 服务实现类
 * </p>
 *
 * @author miao
 * @since 2024-01-02
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopDao, Shop> implements ShopService {

    @Autowired
    private ShopDao dao;

    /**
     * 查询条件
     * @param params
     * @return
     */
    public QueryWrapper<Shop> getQueryWrapper(Map<String,Object> params) {
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        shopQueryWrapper.like(!StringUtils.isEmpty(name),"name",name);
        return shopQueryWrapper;
    }

    /**
     * 添加信息
     *
     * @param entity
     * @return
     */
    @Override
    public int add(Shop entity) {
        return dao.insert(entity);
    }

    /**
     * 编辑信息
     *
     * @param entity
     * @return
     */
    @Override
    public int update(Shop entity) {
        return dao.updateById(entity);
    }

    /**
     * 获取列表信息
     *
     * @param queryWrapper
     * @return
     */

    public List<Shop> selectList(Wrapper<Shop> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    @Override
    public int delete(Long id) {
        int i = dao.deleteById(id);
        return i;
    }
    public PageData<Shop> Page(Map<String,Object> params) {
        QueryWrapper<Shop> queryWrapper = getQueryWrapper(params);
        String page1 = (String) params.get("currentPage");
        String size = (String) params.get("pageSize");
        Page<Shop> page = new Page<>(Integer.parseInt(page1), Integer.parseInt(size));
        Page<Shop> pageInfo = super.page(page, queryWrapper);
        return new PageData<>(pageInfo.getRecords(), pageInfo.getTotal());
    }
}


